
// import axios from 'axios'

import axios from 'axios'
import { Indicator } from 'mint-ui'
import store from '@/store/index'
import router from '@/router'

export default {
  common: {
    method: 'GET',
    params: {},
    data: {},
    headers: {}
  },
  $axios (options = {}) {
    options.method = options.method || this.common.method
    // get
    options.params = options.params || this.common.params
    // data
    options.data = options.data || this.common.data
    options.headers = options.headers || this.common.headers
    // 请求前
    Indicator.open('加载中...')

    // 判断是否传入token
    if (options.headers.token) {
      options.headers.token = store.state.user.token
      if (!options.headers.token) {
        router.push('/login')
      }
    }
    return axios(options).then(v => {
      const data = v.data.data
      return new Promise((resolve, reject) => {
        if (!v) {
          return reject(v)
        }
        setTimeout(() => {
          // 请求结束关闭
          Indicator.close()
        }, 500)
        return resolve(data)
      })
    })
  }
}
